/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager;

import br.com.foxline.model.ActCep;
import br.com.foxline.model.Annex;
import br.com.foxline.model.Charter;
import br.com.foxline.service.ActCepService;
import br.com.foxline.service.CharterService;
import br.com.foxline.util.ConverterImg;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerBackup implements Serializable {

    @EJB
    CharterService charterService;
    @EJB
    ActCepService actCepService;
    private Charter c;
    private List<String> charters;
    private String livrosString = "E127-E124-E049-E207-E123-E135-E028-E220-E061-E020-E126-E051-E141-E146-E250-E192-E105-E079-E008-E017-E131-E114-E042-E137-E091-E147-E092-E200-E077-E165-E042-E139-E224-E204-E189-E110-E084-E054-E107-E223-E242-E186-E033-E161-E256-E246-E232-E254-E151-E176-E212-E175-E170-E225-E237-E240-E243-E038-E029-E047-E177-E118-E218-E178-E032-E216-E078-E143-E113-E249-E106-E217-E006-E201-E235-E229-E022-E059-E159-E251-E144-E088-E181-E129-E045-E164-E130-E040-E053-E173-E152-E095-E157-E185-E142-E244-E112-E221-E014-E247-E190-E066-E133-E149-E158-E202-E024-E227-E004-E210-E055-E072-E128-E048-E068-E041-E011-E231-E096-E183-E257-E116-E015-E012-E069-E252-E050-E044-E258-E239-E016-E083-E259-E007-E171-E203-E115-E134-E075-E193-E226-E213-E260-E064-E174-E195-E021-E001-E043-E230-E101-E070-E099-E167-E056-E228-E037-E117-E155-E148-E180-E081-E166-E145-E253-E067-E140-E094-E025-E122-E206-E215-E199-E093-E052-E211-E169-E063-E194-E245-E184-E057-E136-E238-E036-E058-E160-E102-E018-E182-E191-E080-E085-E222-E098-E062-E163-E010-E074-E214-E125-E120-E208-E153-E209-E219-E168-E027-E172-E031";
    private String livrosStringProcuracao = "L0202-L0417-L0427-L0002-L0352-L0363-L0178-L0450-L0425-L0254-L0242-L0401-L0223-L0438-L0419-L0467-L0340-L0224-L0314-L0195-L0451-L0278-L0229-L0430-L0246-L0210-L0324-L0211-L0493-L0001-L0402-L0266-L0464-L0374-L0321-L0426-L0197-L0285-L0269-L0484-L0220-L0259-L0381-L0236-L0456-L0249-L0213-L0361-L0384-L0482-L0353-L0457-L0305-L0455-L0311-L0441-L0189-L0400-L0454-L0411-L0232-L0230-L0184-L0471-L0300-L0434-L0238-L0325-L0406-L0276-L0346-L0421-L0407-L0442-L0485-L0416-L0286-L0176-L0235-L0252-L0389-L0459-L0486-L0480-L0293-L0444-L0385-L0279-L0476-L0287-L0424-L0005-L0488-L0371-L0173-L0271-L0449-L0290-L0469-L0212-L0241-L0377-L0007-L0499-L0478-L0177-L0364-L0347-L0414-L0288-L0198-L0498-L0350-L0294-L0003-L0203-L0267-L0204-L0222-L0375-L0181-L0273-L0387-L0006-L0206-L0308-L0483-L0215-L0334-L0261-L0253-L0240-L0208-L0448-L0251-L0404-L0333-L0501-L0415-L0209-L0376-L0391-L0409-L0394-L0370-L0398-L0470-L0303-L0357-L0182-L0194-L0264-L0317-L0495-L0298-L0343-L0183-L0341-L0435-L0439-L0326-L0217-L0295-L0190-L0172-L0437-L0174-L0355-L0216-L0420-L0475-L0368-L0388-L0465-L0289-L0473-L0318-L0383-L0250-L0366-L0322-L0310-L0329-L0336-L0328-L0331-L0256-L0221-L0369-L0192-L0243-L0292-L0463-L0327-L0358-L0272-L0205-L0428-L0304-L0239-L0323-L0395-L0191-L0277-L0373-L0335-L0410-L0247-L0392-L0367-L0492-L0491-L0180-L0481-L0330-L0354-L0186-L0412-L0339-L0378-L0436-L0244-L0397-L0497-L0337-L0257-L0408-L0458-L0452-L0443-L0351-L0422-L0393-L0487-L0260-L0490-L0255-L0338-L0348-L0460-L0405-L0379-L0479-L0179-L0356-L0219-L0345-L0315-L0496-L0360-L0004-L0472-L0283-L0200-L0418-L0403-L0466-L0462-L0270-L0445-L0185-L0344-L0299-L0234-L0342-L0263-L0500-L0494-L0199-L0440-L0226-L0233-L0365-L0446-L0282-L0477-L0362-L0372-L0332-L0268-L0302-L0359-L0262-L0275-L0297-L0390-L0228-L0447-L0193-L0218-L0187-L0468-L0396-L0431-L0207-L0188-L0309-L0237-L0453-L0296-L0386-L0201-L0248-L0319-L0227-L0214-L0306-L0274-L0301-L0349-L0231-L0307-L0316-L0258-L0461-L0399-L0312-L0429-L0433-L0313-L0281-L0284-L0291-L0489-L0196-L0432-L0474-L0413-L0175-L0423-L0225-L0245-L0382-L0380-L0265-L0280-L0320-";
    
    private String li[];
    
    @PostConstruct
    public void init() {
        charters = new ArrayList<String>();
        
    }

    public void backupProcuracao() {
        StringBuilder sb = new StringBuilder();
        try{
        init();
        li = livrosStringProcuracao.split("-");
        
        int cont = 0;
        int contArq = 0;
        int aux = 0;
        String url;// = "/opt/ARQUIVO CARTORIO/PROCURACAO/LIVROS";
        url="/data/imagem/PROCURACAO/LIVROS/";
        
        File file = new File(url);
        FileInputStream fileInputStream = null;
        Annex annex;
        System.err.println("pesquisando...");

        List<String> listLivros = Arrays.asList(li);
        
        for (File livros : file.listFiles()) {
            if (!listLivros.contains(livros.getName())) {
                
                aux++;
                if (aux > 30) {
                    System.err.print("LIVROS #:"+aux);
                    break;
                }
                System.err.print("Livro: "+livros.getName());
                sb.append(livros.getName() + "-");
                
                for (File num : livros.listFiles()) {
                    
                    if (num.isDirectory()) {

                        for (File arq : num.listFiles()) {
                            contArq++;

                            if ((arq.getName().matches(".*\\.tif?f"))
                                    || (arq.getName().matches(".*\\.pdf"))
                                    || (arq.getName().matches(".*\\.jpe?g"))) {

                                c = charterService.charterBackupAnnexProcuracao(livros.getName().replace("L0", ""),
                                        livros.getName().replaceAll("\\d*", ""), actCepService.find(2L), num.getName().replaceAll("\\D*", ""));

                                annex = new Annex();
                                annex.setDescription(arq.getName());
                                if (c != null) {
                                    System.err.print(" ID = "+c.getId());
                                    cont++;
                                    charters.add(livros.getName() + " - " + num.getName() + " - " + arq.getName());

                                    if (arq.getName().matches(".*\\.tif?f")) {
                                        try {
                                            fileInputStream = new FileInputStream(arq);
                                            annex.setFileByte(ConverterImg.convertTiffToJpg(fileInputStream));
                                        } catch (FileNotFoundException ex) {
                                            Logger.getLogger(ManagerBackup.class.getName()).log(Level.SEVERE, null, ex);
                                        }
//
                                    } else if (arq.getName().matches(".*\\.pdf")) {
                                        annex.setType("Application/PDF");
                                    } else if (arq.getName().matches(".*\\.jpe?g")) {
                                        annex.setType("Image/JPEG");
                                    }

                                    if (annex.getFileByte() == null) {
                                        byte[] bFile = convert(arq);
                                        annex.setFileByte(bFile);
                                    }

                                    if (c.getAnnex() != null) {
                                        boolean eq = false;
                                        for (Annex a : c.getAnnex()) {
                                            if (a.getDescription().equals(annex.getDescription())) {
                                                eq = true;
                                                System.err.println("IGUALL&&&&&&&&&&&&&: "+ livros.getName()+" - "+num.getName());
                                                break;
                                            }
                                        }
                                        if (!eq) {
                                            c.getAnnex().add(annex);
                                        }
                                    } else {
                                        List<Annex> lst = new ArrayList<Annex>();
                                        lst.add(annex);
                                        c.setAnnex(lst);
                                    }
                                    charterService.update(c);
                                }
                            }
                        }
                    }

                }
            }else{
                System.err.println("LIVRO JÁ FOI: "+livros.getName());
                
            }
        }
        System.err.println(sb);
        livrosStringProcuracao = livrosStringProcuracao + " # "+sb.toString();
        }catch(Exception e){
            e.printStackTrace();
            livrosStringProcuracao = livrosStringProcuracao + " # "+sb.toString();
            System.err.println(" stringbuilder: "+sb);
            System.err.println(" livrosstring: "+livrosStringProcuracao);
        }
        
        
    }
    
    public void backup() {
        init();
        li = livrosString.split("-");
        
        int cont = 0;
        int contArq = 0;
        int aux = 0;
        String url;// = "/opt/ARQUIVO CARTORIO/ESCRITURAS/";
        url="/data/imagem/ESCRITURAS/";
        StringBuilder sb = new StringBuilder();
        File file = new File(url);
        FileInputStream fileInputStream = null;
        Annex annex;
        System.err.println("pesquisando...");

        List<String> listLivros = Arrays.asList(li);
        //E127 - E124 - E049 - E207 - E123 - E135 - E028 - E220 - E061 - E020 - E126 - E051 - E141 - E146 - E250 - E192 - E105 - E079 - E008 - E017
        for (File livros : file.listFiles()) {
            if (!listLivros.contains(livros.getName())) {
                
                aux++;
                if (aux > 30) {
                    System.err.print("LIVROS #:"+aux);
                    break;
                }
                System.err.print("Livro: "+livros.getName());
                sb.append(livros.getName() + "-");
                for (File num : livros.listFiles()) {
                    
                    if (num.isDirectory()) {

                        for (File arq : num.listFiles()) {
                            contArq++;

                            if ((arq.getName().matches(".*\\.tif?f"))
                                    || (arq.getName().matches(".*\\.pdf"))
                                    || (arq.getName().matches(".*\\.jpe?g"))) {

                                c = charterService.charterBackupAnnex(livros.getName().replaceAll("\\D*", ""),
                                        livros.getName().replaceAll("\\d*", ""), num.getName());

                                annex = new Annex();
                                annex.setDescription(arq.getName());
                                if (c != null) {
                                    cont++;
                                    charters.add(livros.getName() + " - " + num.getName() + " - " + arq.getName());

                                    if (arq.getName().matches(".*\\.tif?f")) {
                                        try {
                                            fileInputStream = new FileInputStream(arq);
                                            annex.setFileByte(ConverterImg.convertTiffToJpg(fileInputStream));
                                        } catch (FileNotFoundException ex) {
                                            Logger.getLogger(ManagerBackup.class.getName()).log(Level.SEVERE, null, ex);
                                        }
//
                                    } else if (arq.getName().matches(".*\\.pdf")) {
                                        annex.setType("Application/PDF");
                                    } else if (arq.getName().matches(".*\\.jpe?g")) {
                                        annex.setType("Image/JPEG");
                                    }

                                    if (annex.getFileByte() == null) {
                                        byte[] bFile = convert(arq);
                                        annex.setFileByte(bFile);
                                    }

                                    if (c.getAnnex() != null) {
                                        boolean eq = false;
                                        for (Annex a : c.getAnnex()) {
                                            if (a.getDescription().equals(annex.getDescription())) {
                                                eq = true;
                                                System.err.println("IGUALL&&&&&&&&&&&&&: "+ livros.getName()+" - "+num.getName());
                                                break;
                                            }
                                        }
                                        if (!eq) {
                                            c.getAnnex().add(annex);
                                        }
                                    } else {
                                        List<Annex> lst = new ArrayList<Annex>();
                                        lst.add(annex);
                                        c.setAnnex(lst);
                                    }
                                    charterService.update(c);
                                }
                            }
                        }
                    }

                }
            }else{
                System.err.println("LIVRO JÁ FOI: "+livros.getName());
                
            }
        }
        System.err.println(sb);
        livrosString = livrosString + " # "+sb.toString();
    }

    public byte[] convert(File arq) {
        byte[] bFile = new byte[(int) arq.length()];

        try {
            FileInputStream fileInputStream = new FileInputStream(arq);
            fileInputStream.read(bFile);
            fileInputStream.close();
        } catch (IOException ex) {
            Logger.getLogger(ManagerBackup.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bFile;
    }

    public List<String> getCharters() {
        return charters;
    }

    public void setCharters(List<String> charters) {
        this.charters = charters;
    }

    public String getLivrosString() {
        return livrosString;
    }

    public void setLivrosString(String livrosString) {
        this.livrosString = livrosString;
    }

    public String getLivrosStringProcuracao() {
        return livrosStringProcuracao;
    }

    public void setLivrosStringProcuracao(String livrosStringProcuracao) {
        this.livrosStringProcuracao = livrosStringProcuracao;
    }
    
    
}
